//
//  RegistTableViewController.m
//  OrderMeal
//
//  Created by 江苏宏创 on 14-7-25.
//  Copyright (c) 2014年 qkong. All rights reserved.
//

#import "RegistTableViewController.h"
#import "SendHttpOperation.h"
@interface RegistTableViewController ()<UITextFieldDelegate>

@end

@implementation RegistTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)checkInput{
    NSString *errorMsg;
    NSArray *sexArray = @[@"男",@"女"];
    if (self.email.text.length == 0) {
        errorMsg = @"邮箱不能为空";
    }else if (self.username.text.length == 0){
        errorMsg = @"用户名不能为空";
    }else if (self.sex.text.length == 0){
        errorMsg = @"性别不能为空";
    }else if (![sexArray containsObject:self.sex.text]){
        errorMsg = @"请填写正确的性别";
    }else if (self.phone.text.length == 0){
        errorMsg = @"电话不能为空";
    }else if (self.buildingId.text.length == 0){
        errorMsg = @"大厦号不能为空";
    }else if (self.location.text.length == 0){
        errorMsg = @"地址不能为空";
    }else if (self.password.text.length == 0){
        errorMsg = @"密码不能为空";
    }else if (self.surePassword.text.length == 0){
        errorMsg = @"确认不能为空";
    }else if (![self.password.text isEqualToString:self.surePassword.text]){
        errorMsg = @"两次密码不同";
    }
    
    if (errorMsg != nil) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"errorMsg" object:nil userInfo:@{@"ERROR":errorMsg}];
        return NO;
    }else{
        return YES;
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if (textField != self.surePassword) {
        UITextField *nextField = (UITextField *)[self.view viewWithTag:textField.tag+1];
        [nextField becomeFirstResponder];
    }else{
        [self.view endEditing:YES];
    }
    return YES;
}


- (IBAction)registAction:(id)sender {
    [self.view endEditing:YES];
    if ([self checkInput]) {
        NSString *sex = [self.sex.text isEqualToString:@"男"]?@"男":@"女";
        [[SendHttpOperation sharedHttpOperation]registWithEmail:self.email.text
                                                       lastName:self.username.text
                                                            sex:sex phone:self.phone.text
                                                     buildingid:self.buildingId.text
                                                        address:self.phone.text
                                                       password:self.password.text];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        UITextField *nowField = (UITextField *)[self.view viewWithTag:indexPath.row+10];
        [nowField becomeFirstResponder];
    }
}


@end
